package com.radon.position.test;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.http.HttpGlobalConfig;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.radon.position.util.CSVUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class Q {

    public static List<String> proxyList = null;
    public static int f = 0;

    public static void main(String[] args) {


        String[] headers = {"名称","区","类型","商圈","地址","电话","原经度","原纬度","经度","纬度","详细信息"};

        String agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36";
        for (int i = 50; i < 185; i++) {
            List<String> names = CollUtil.newArrayList();
            List<String> hrefs = CollUtil.newArrayList();

            HttpGlobalConfig.setTimeout(3000);
            String ip = null;
            Integer port = null;

            String s2 = null;
            JSONObject jsonObject = null;
            String proxy = null;
            String[] split1 = null;
            int z = 0;
            List<String[]> content = new ArrayList<>();

            try {
                String s = null;
                Document html = null;
                Elements maplist = null;

                z++;

                if (z%10 == 1){
                    s2 = getProxy();
                    jsonObject = JSON.parseObject(s2);
                    proxy = jsonObject.getString("proxy");
                    String[] split2 = proxy.split(":");
                    ip = split2[0];
                    port = Integer.valueOf(split2[1]);

                    s = get("http://www.iecity.com/shenzhen/life/Cate-792-815------------------"+(i+1)+".html",ip,port);
                    if (StrUtil.isBlank(s)||!s.contains("iecity")){
                        z = 9;
                        i--;
                        proxyList.remove(s2);
                        f--;
                        continue;
                    }
                    html = Jsoup.parse(s);

                    maplist = html.select(".LifeList a");
                    if (maplist.size() == 0){
                        i--;
                        continue;
                    }
                }


                for (int j = 0; j < maplist.size(); j++) {
                    String info = null;
                    try {
                        z++;
                        if (z%10 == 1){
                            s2 = getProxy();
                            jsonObject = JSON.parseObject(s2);
                            proxy = jsonObject.getString("proxy");
                            String[] split2 = proxy.split(":");
                            ip = split2[0];
                            port = Integer.valueOf(split2[1]);
                            s = get("http://www.iecity.com/shenzhen/life/Cate-792-815------------------"+(i+1)+".html",ip,port);
                            if (StrUtil.isBlank(s)||!s.contains("iecity")){
                                z = 9;
                                j--;
                                proxyList.remove(s2);
                                f--;
                                continue;
                            }
                            //html = Jsoup.parse(s);
                            //maplist = html.select(".LifeList a");
                        }
                        String[] line = new String[11];
                        Element element = maplist.get(j);
                        String name = element.text();
                        String href = element.attr("href");
                        if (!hrefs.contains(href)){
                            names.add(name);
                            hrefs.add(href);
                        }

                        info = get(href,ip,port);
                        if (StrUtil.isBlank(info)||!info.contains("iecity")){
                            j--;
                            continue;
                        }
                        Document infoHtml = Jsoup.parse(info);
                        Elements infoList = infoHtml.select("#content tr");
                        if (infoList.size()==0){
                            String script = "<script>DW\\('(.*?)'\\)<\\/script>";
                            List<String> scripts = ReUtil.findAll(script, info, 1);
                            info = ReUtil.replaceAll(info, script, StrUtil.replace(HtmlUtil.unescape(scripts.get(0)),"\\'","'"));
                            infoHtml = Jsoup.parse(info);
                            infoList = infoHtml.select("#content tr");
                        }
                        if (infoList.size()==0){
                            j--;
                            continue;
                        }
                        Map<String,String> infoMap = new HashMap<>();
                        for (int k = 0; k < infoList.size(); k++) {
                            String key = infoList.get(k).children().get(0).text();
                            if ("最后更新：".equals(key)){
                                infoMap.put(key,infoList.get(k).children().get(1).children().get(0).children().get(0).text());
                            }else {
                                infoMap.put(key,infoList.get(k).children().get(1).children().get(0).text());
                            }
                        }
                        line[0] = name;
                        line[1] = infoMap.get("商户类型：");
                        line[2] = infoMap.get("所在地区：");
                        line[3] = infoMap.get("所在商圈：");
                        line[4] = infoMap.get("具体地址：");
                        line[5] = infoMap.get("联系电话：");
                        line[10] = infoList.toString();



                        String pattern = "CreateMap\\((.+)\\)";
                        String s1 = regMatch(info, pattern);
                        if (StrUtil.isNotBlank(s1)){
                            s1 = StrUtil.removeAll(s1,"CreateMap(\"mapObj\",\"");
                            s1 = StrUtil.removeAll(s1,"\",16,\""+name+"\")");
                            String[] split = s1.split("\",\"");
                            double[] doubles = Test.gcj02_To_Gps84(Double.valueOf(split[1]), Double.valueOf(split[0]));

                            line[6] = String.valueOf(split[0]);
                            line[7] = String.valueOf(split[1]);
                            line[8] = String.valueOf(doubles[1]);
                            line[9] = String.valueOf(doubles[0]);
                        }else {
                            String lngPattern = "var lng = \"(.*?)\"\\r\\n";
                            String latPattern = "var lat = \"(.*?)\"\\r\\n";
                            List<String> lng = ReUtil.findAll(lngPattern, info, 1);
                            List<String> lat = ReUtil.findAll(latPattern, info, 1);
                            if (CollUtil.isNotEmpty(lng)&&CollUtil.isNotEmpty(lat)){
                                double[] doubles = Test.gcj02_To_Gps84(Double.valueOf(lat.get(0)), Double.valueOf(lng.get(0)));
                                line[6] = String.valueOf(lng.get(0));
                                line[7] = String.valueOf(lat.get(0));
                                line[8] = String.valueOf(doubles[1]);
                                line[9] = String.valueOf(doubles[0]);
                            }else {
                                System.out.println("111");
                            }
                        }

                        content.add(line);
                        names.remove(name);
                        hrefs.remove(href);
                    }catch (Exception c){
                        j--;/*
                        System.out.println("");
                        System.out.println(info);
                        System.out.println("");
                        System.out.println(ip+":"+port);
                        System.out.println("");
                        c.printStackTrace();*/
                    }

                }
            }catch (Exception e){
                i--;
                continue;/*

                e.printStackTrace();*/
            }

            System.out.println(JSON.toJSONString(names));
            System.out.println(JSON.toJSONString(hrefs));
            CSVUtil.write("C:\\Users\\12903\\Desktop\\xq\\"+"桥梁"+i+".csv",content,headers);

        }

    }

    public static String get(String url,String ip,Integer port) {
        String agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36";
        String res = null;
        for (int i = 0; i < 10; i++) {
            try {
                res = HttpUtil.createGet(url).setProxy(new Proxy(Proxy.Type.HTTP,
                        new InetSocketAddress(ip, port))).header("User-Agent",agent).execute().body();
                return res;
            }catch (Exception e){

            }
        }
        return res;
    }


    public static String regMatch(String withinText, String regString) {
        String code = null;
        Pattern pattern = Pattern.compile(regString);
        Matcher matcher = pattern.matcher(withinText);
        if (matcher.find()) {
            matcher.reset();
            while (matcher.find()) {
                return matcher.group(0);
            }
        }
        return code;
    }

    public static int dl(List<List<String>> dlList,int page){
        String dl = HttpUtil.get("https://www.kuaidaili.com/free/inha/"+page+"/");
        Document dlHtml = Jsoup.parse(dl);
        Elements dlTd = dlHtml.select(".table tr");
        boolean flag = false;
        for (int i = 1; i < dlTd.size(); i++) {
            Element element = dlTd.get(i);
            String ip = element.children().get(0).text();
            String port = element.children().get(1).text();
            try {
                String info = HttpUtil.createGet("https://www.baidu.com/")
                        .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, Integer.parseInt(port))))
                        .timeout(2000)
                        .execute()
                        .body();
            }catch (Exception e){
                e.printStackTrace();
                continue;
            }

            List<String> next = CollUtil.newArrayList();
            next.add(ip);
            next.add(port);
            dlList.add(next);
            flag = true;
        }
        if (!flag){
            page = dl(dlList,++page);
        }
        return page;
    }

    public static int dl1(List<List<String>> dlList,int page){
        String dl = HttpUtil.get("https://www.89ip.cn/index"+page+".html");
        Document dlHtml = Jsoup.parse(dl);
        Elements dlTd = dlHtml.select(".layui-table tr");
        boolean flag = false;
        for (int i = 1; i < dlTd.size(); i++) {
            Element element = dlTd.get(i);
            String ip = element.children().get(0).text();
            String port = element.children().get(1).text();
            try {
                String info = HttpUtil.createGet("https://www.baidu.com/")
                        .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, Integer.parseInt(port))))
                        .timeout(2000)
                        .execute()
                        .body();
            }catch (Exception e){
                e.printStackTrace();
                continue;
            }

            List<String> next = CollUtil.newArrayList();
            next.add(ip);
            next.add(port);
            dlList.add(next);
            flag = true;
        }
        if (!flag){
            page = dl1(dlList,++page);
        }
        return page;
    }

    public static String getProxy(){
        if (CollUtil.isEmpty(proxyList) || f >= proxyList.size()){
            String s = HttpUtil.get("http://82.156.28.207:3000/all/");
            JSONArray objects = JSON.parseArray(s);
            proxyList = objects.stream().map(next->{
                return JSON.toJSONString(next);
            }).collect(Collectors.toList());

            f = 0;
        }

        String s = proxyList.get(f);
        f++;
        return s;
    }
}
